package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("region")
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @RequestMapping("list")
    public String list(){
        return "region/list";
    }

    @RequestMapping("selectByParentId")
    @ResponseBody
    public List selectByParentId(Long parentId,String type){//type标记是获取treeview的数据,还是table的数据
        List<Region> regions = regionService.selectByParentId(parentId);
        if("tree".equals(type)){
            ArrayList<Map> list = new ArrayList<>();
            // 把每个地区对象转成map,添加前端插件需要的属性
            for (Region region : regions) {
                list.add(region.toTreeMap());
            }
            return list;
        }else{
            return regions;
        }
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Region region){
        regionService.saveOrUpdate(region);
        return new JsonResult();
    }

    @RequestMapping("updateState")
    @ResponseBody
    public JsonResult updateState(Long id,Integer state){
        regionService.updateState(id,state);
        return new JsonResult();
    }



}
